package chapter3_array;

import java.util.Arrays;

/**
 * 指针对撞 两个数字求和
 */
public class PointerColision {

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4,5,6,7};
        System.out.println(Arrays.toString(twoSum(arr, 8)));
    }

    public static int[] twoSum(int[] nums, int target) {
        int[] rt = new int[2];

        int l = 0, r = nums.length - 1;
        while (l < r) {
            if (nums[l] + nums[r] > target) {
                r--;
            } else if (nums[l] + nums[r] < target) {
                l++;
            } else {
                break;
            }
        }
        rt[0] = l + 1;
        rt[1] = r + 1;
        return rt;
    }
}
